# 交叉编译工具链配置 (可根据目标平台修改)
USE_CROSS_COMPILE ?= 1
CROSS_COMPILE ?= /media/test/sde/ubuntu2204/TL3568F-AMP/rk356x_linux_release_v1.3.1/prebuilts/gcc/linux-x86/aarch64/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-

ifeq ($(USE_CROSS_COMPILE),1)
    CC := $(CROSS_COMPILE)gcc
    STRIP := $(CROSS_COMPILE)strip
else
    CC := gcc
    STRIP := strip
endif

# 项目配置
TARGET = pcie_cdev_ad_test
SRC_DIR = $(PWD)
OBJ_DIR = $(PWD)
INC_DIR = $(PWD)
SRCS = $(wildcard $(SRC_DIR)/*.c)
OBJS = $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRCS))

# 编译选项
CFLAGS = -I$(INC_DIR) -Wall -Werror -O2
LDFLAGS = -lm -lpthread
DEBUG_CFLAGS = -g -DDEBUG
RELEASE_CFLAGS = -s -DNDEBUG

# 默认编译目标
all: release

# 调试版本
debug: CFLAGS += $(DEBUG_CFLAGS)
debug: $(TARGET)

# 发布版本
release: CFLAGS += $(RELEASE_CFLAGS)
release: $(TARGET)
	$(STRIP) $(TARGET)

# 主目标链接
$(TARGET): $(OBJS)
	$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)

# 生成对象文件
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR)
	$(CC) $(CFLAGS) -c $< -o $@

# 创建对象目录
$(OBJ_DIR):
	mkdir -p $@

# 交叉编译目标
cross: export USE_CROSS_COMPILE = 1
cross: release

# 清理
clean:
	rm -f $(TARGET) $(OBJS)

# 安装到目标系统 (示例路径)
install: $(TARGET)
	scp $(TARGET) user@target:/usr/local/bin

.PHONY: all debug release clean install cross
